/*
提交链接:https://leetcode.cn/problems/permutations-ii/description/
47. 全排列 II
赖德檀 2024/12/28
*/

class Solution {
public:

    vector<vector<int>> permuteUnique(vector<int>& nums) {
        vector<vector<int>>ans;
        f(nums,0,ans);
        return ans;
        }
        void f(vector<int>&nums,int i,vector<vector<int>>&ans)
        {
            if(i==nums.size())
            ans.push_back(nums);
            else
            {               
                 set<int>it;
                for(int j=i;j<nums.size();j++)
                {
                    if(!it.count(nums[j]))
                    {
                    it.insert(nums[j]);
                    swap(nums[i],nums[j]);
                    f(nums,i+1,ans);
                    swap(nums[i],nums[j]);
                    }
                }
            }
        }
};